﻿using System;
using Loki.Data;
using Loki.Model;
using Luna.Data.Configuration;

namespace Luna.Model
{
    public abstract class LunaService : ApplicationService
    {
        protected void Delete<TEdit, TRead, TWrite, TEntityData>(TWrite entity, Action<Guid> dataDelete, EntityContainer<TRead, Guid> store)
            where TRead : ILunaEntity
            where TWrite : ILunaEntity
            where TEdit : LunaEntity<TEntityData>, TWrite, TRead
            where TEntityData : class, IDataEntity<Guid>
        {
            TEdit convertedEntity = entity as TEdit;
            if (convertedEntity != null)
            {
                dataDelete(convertedEntity.Data.PK);

                store.Remove(convertedEntity);
            }
        }

        protected void Delete<TEdit, TRead, TWrite, TEntityData>(TWrite entity, Action<IReferenceData> dataDelete, EntityContainer<TRead, Guid> store)
            where TRead : ILunaEntity
            where TWrite : ILunaEntity
            where TEdit : LunaEntity<TEntityData>, TWrite, TRead
            where TEntityData : class,IReferenceData
        {
            TEdit convertedEntity = entity as TEdit;
            if (convertedEntity != null)
            {
                dataDelete(convertedEntity.Data);

                store.Remove(convertedEntity);
            }
        }

        protected void Save<TEdit, TRead, TWrite, TEntityData>(TWrite entity, Action<TEntityData> dataSave, EntityContainer<TRead, Guid> store)
            where TRead : ILunaEntity
            where TWrite : ILunaEntity
            where TEdit : LunaEntity<TEntityData>, TWrite, TRead
            where TEntityData : class, IDataEntity<Guid>
        {
            TEdit convertedEntity = entity as TEdit;
            if (convertedEntity != null)
            {
                dataSave(convertedEntity.Data);

                store.Register(convertedEntity);
            }
        }
    }
}